package com.glowdayz.glowmee.bc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.cmd.AdminTableCMD;
import com.glowdayz.glowmee.dao.AdminTableDAO;

public class ListNoticeBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListNoticeBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String id_str = request.getParameter("id");
		String isDisplay_str = request.getParameter("isDisplay");
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		if (order != null && order.equals("isUnDisplay")) {
			order = "isDisplay ASC, create_date DESC";
			request.setAttribute("order", "&order=isUnDisplay");
		} else if (order != null && order.equals("isDisplay")) {
			order = "isDisplay DESC, create_date DESC";
			request.setAttribute("order", "&order=isDisplay");
		}
		
		if (id_str != null && isDisplay_str != null) {
			int id = Integer.parseInt(id_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			
			AdminTableCMD cmd = new AdminTableCMD(id, isDisplay);
			try {
				AdminTableDAO.getInstance().isDisplayAdminTable(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("notice", AdminTableDAO.getInstance().getNoticeList(page, 10, "NOTICE", order));
			request.setAttribute("display", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='NOTICE' AND isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='NOTICE' AND isDisplay='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listNotice.jsp");
		dpr.forward(request, response);
	}

	/** 공지사항을 추가할때 실행되는 메서드 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		MultipartBox mbox = MultipartBox.getInstance();
		FileInfo finfo = mbox.upload2(request);
		
		String boardTitle = mbox.getValue("boardTitle");
		String boardText = mbox.getValue("boardText");
		
		try {
			AdminTableDAO.getInstance().insertNotice(new AdminTableCMD(boardTitle, boardText, finfo));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("notice", AdminTableDAO.getInstance().getNoticeList(1, 10, "NOTICE", null));
			request.setAttribute("display", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='NOTICE' AND isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='NOTICE' AND isDisplay='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listNotice.jsp");
		dpr.forward(request, response);
	}

}